package com.cash.app.ui.order

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.cash.app.base.BaseViewModel
import com.cash.app.manager.api.GlobalAPIService
import com.cash.app.manager.model.FastApplyResponse
import com.cash.app.manager.model.OrderIdRequest
import com.cash.app.manager.model.ProductIdRequest
import com.cash.app.manager.model.RepayResponse
import com.cash.app.net.NetRequestExecutor
import com.cash.app.net.RetrofitClient
import com.cash.app.ui.order.api.LoanOrderAPIService
import com.cash.app.ui.order.model.LoanDetailResponse

class LoanInfoViewModel : BaseViewModel() {

    private val loanOrderAPIService by lazy {
        RetrofitClient.createService(LoanOrderAPIService::class.java)
    }

    private val globalAPIService by lazy {
        RetrofitClient.createService(GlobalAPIService::class.java)
    }
    private val _loanInfoLiveData = MutableLiveData<LoanDetailResponse>()
    val loanInfo: LiveData<LoanDetailResponse> = _loanInfoLiveData

    private val _repayInfoLiveData = MutableLiveData<RepayResponse>()
    val repayInfo: LiveData<RepayResponse> = _repayInfoLiveData

    private val _fastApplyData = MutableLiveData<FastApplyResponse>()
    val fastApplyData: LiveData<FastApplyResponse> = _fastApplyData

    fun getLoanDetail(orderId: Int) {
        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                loanOrderAPIService.getLoanDetail(OrderIdRequest(orderId))
            }
        }, onSuccess = { response ->
            response?.let {
                _loanInfoLiveData.value = response!!
            }
        })
    }

    fun getRepayUrl(orderId: Int) {
        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                globalAPIService.getRepayUrl(OrderIdRequest(orderId))
            }
        }, onSuccess = { response ->
            response?.let {
                _repayInfoLiveData.value = response!!
            }
        })
    }

    fun fastApply(productId: Int) {
        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                globalAPIService.fastApply(ProductIdRequest(productId))
            }
        }, onSuccess = { response ->
            response?.let {
                _fastApplyData.value = it
            }
        })
    }
} 